<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Support\Facades\Input;
use App\Http\Controllers\Controller;
use \Illuminate\Http\Request;
use DB;
use Crypt;
  class BasicsController extends CommonController{
    /**
     * 网站信息页面渲染
     * @return [type] [description]
     */
    public function info(){
      $data = DB::table('mo_basics')->where(array('b_id'=>1))->first();
      return view('admin.basics.info',array('data'=>$data));
    }
    /**
     * 修改密码页面渲染
     * @return [type] [description]
     */
    public function change_pass(){
            return view('admin.basics.pass');
    }
    /**
     * 首页轮播页面渲染
     * @return [type] [description]
     */
    public function adv(){
      $data = DB::table('mo_flashview')->get();
      return view('admin.basics.adv',array('data'=>$data));
    }
    /**
     * 网站信息添加
     * @return [type] [description]
     */
    public function infoadd(){
      $data = Input::all();
      $bool= Storage::disk('upload')->put($filename,file_get_contents($path));
      $res = DB::table('mo_basics')->insert([
        'b_stitle' => $data['b_stitle'],
        'b_slogo' => $data['b_slogo'],
        'b_surl' => $data['b_surl'],
        'b_skeywords' => $data['b_skeywords'],
        'b_sdescription' => $data['b_sdescription'],
        'b_s_name' => $data['b_s_name'],
        'b_s_phone' => $data['b_s_phone'],
        'b_s_tel' => $data['b_s_tel'],
        'b_s_qq' => $data['b_s_qq'],
        'b_s_email' => $data['b_s_email'],
      ]);
      return view('admin.basics.info');
    }
    /**
     * 网站信息修改
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function infoupd(Request $request){
      $data = Input::all();
      $file = $request->file('b_slogo');
      if($file){
        $allowed_extensions = ["png", "jpg", "gif","jpeg","JPG","PNG","GIF","JPEG"];
        $destinationPath = 'admin_style/uploads/'; //public 文件夹下面建 storage/uploads 文件夹
        $extension = $file -> getClientOriginalExtension(); //4、获取上传图片的后缀名
        if(in_array($extension,$allowed_extensions)){
          $data['b_slogo']= '/'.md5(time()).random_int(5,5).".".$extension;//5、重新命名上传文件名字
          $file->move($destinationPath,$data['b_slogo']);
          $res = DB::table('mo_basics')->where(array('b_id'=>$data['id']))->update([
            'b_stitle' => $data['b_stitle'],
            'b_slogo' => $data['b_slogo'],
            'b_surl' => $data['b_surl'],
            'b_skeywords' => $data['b_skeywords'],
            'b_sdescription' => $data['b_sdescription'],
            'b_s_name' => $data['b_s_name'],
            'b_s_phone' => $data['b_s_phone'],
            'b_s_tel' => $data['b_s_tel'],
            'b_s_qq' => $data['b_s_qq'],
            'b_s_email' => $data['b_s_email'],
          ]);
        }else{
          $title = array('content'=>'图片格式不正确','url'=>'info');
          return view('admin.alert',['data'=>$title]); 
        }
      }else{
        $res = DB::table('mo_basics')->where(array('b_id'=>$data['id']))->update([
          'b_stitle' => $data['b_stitle'],
          'b_slogo' => $data['blogo'],
          'b_surl' => $data['b_surl'],
          'b_skeywords' => $data['b_skeywords'],
          'b_sdescription' => $data['b_sdescription'],
          'b_s_name' => $data['b_s_name'],
          'b_s_phone' => $data['b_s_phone'],
          'b_s_tel' => $data['b_s_tel'],
          'b_s_qq' => $data['b_s_qq'],
          'b_s_email' => $data['b_s_email'],
        ]);
      }
      return redirect('basics/info');
    }
    /**
     * 修改密码
     * @return [type] [description]
     */
    public function pasupd(){
      $data = Input::all();
      $user = DB::table('mo_huser')->where(array('u_name'=>$data['u_name']))->first();
      $pwd = Crypt::decrypt($user->u_password);
      if($data['mpass'] == $pwd){
        $pas = Crypt::encrypt($data['newpass']);
        $res = DB::table('mo_huser')->where(array('u_name'=>$data['u_name']))->update([
          'u_password' => $pas,
        ]);
        echo "<script>alert('密码修改成功，请重新登录!');parent.location.href='login';</script>"; 
      }else{
        return redirect('basics/change_pass');
      }
    }
    /**
     * 首页轮播添加
     * @return [type] [description]
     */
    public function advadd(Request $request){
      $data = Input::all();
      $file = $request->file('a_img');
      $allowed_extensions = ["png", "jpg", "gif"];
      $destinationPath = 'admin_style/image/'; //public 文件夹下面建 storage/uploads 文件夹   
      $extension = $file->getClientOriginalExtension(); //4、获取上传图片的后缀名
      $data['a_img']= '/'.md5(time()).random_int(5,5).".".$extension;//5、重新命名上传文件名字
      $file->move($destinationPath,$data['a_img']);
      $res = DB::table('mo_flashview')->insert([
        'a_title' => $data['a_title'],
        'a_url' => $data['a_url'],
        'a_img' => $data['a_img'],
        'a_note' => $data['a_note'],
        'a_sort' => $data['a_sort'],
      ]);
      return redirect('basics/adv');
    }
    /**
     * 首页轮播预修改
     * @return [type] [description]
     */
    public function advupd($id){
      $data = DB::table('mo_flashview')->where(array('a_id'=>$id))->first();
      return view('admin.basics.advupd',array('upd'=>$data));
    }
    /**
     * 首页轮播修改
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function advupd_do(Request $request){
      $data = Input::all();
      $file = $request->file('a_img');
      if($file){
        $allowed_extensions = ["png", "jpg", "gif"];
        $destinationPath = 'admin_style/image/'; //public 文件夹下面建 storage/uploads 文件夹
        $extension = $file -> getClientOriginalExtension(); //4、获取上传图片的后缀名
        $data['a_img']= '/'.md5(time()).random_int(5,5).".".$extension;//5、重新命名上传文件名字
        $file->move($destinationPath,$data['a_img']);
        $res = DB::table('mo_flashview')->where(array('a_id'=>$data['id']))->update([
          'a_title' => $data['a_title'],
          'a_url' => $data['a_url'],
          'a_img' => $data['a_img'],
          'a_note' => $data['a_note'],
          'a_sort' => $data['a_sort'],
        ]);
      }else{
        $res = DB::table('mo_flashview')->where(array('a_id'=>$data['id']))->update([
          'a_title' => $data['a_title'],
          'a_url' => $data['a_url'],
          'a_img' => $data['a_img'],
          'a_note' => $data['a_note'],
          'a_sort' => $data['a_sort'],
        ]);
      }
      return redirect('basics/adv');
    }
    public function advdel($id){
      $res = DB::table('mo_flashview')->where(array('a_id'=>$id))->delete();
      if($res){
        return redirect('basics/adv');
      }else{
        return redirect()->back()->withInput()->withErrors('删除失败');
      }
    }
  }
?>       
